package com.singzi.hospappo.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.singzi.hospappo.cmn.client.DictFeignClient;
import com.singzi.hospappo.hosp.respository.HospitalRepository;
import com.singzi.hospappo.model.hosp.Hospital;
import com.singzi.hospappo.hosp.respository.HospitalRepository;
import com.singzi.hospappo.hosp.service.HospitalService;
import com.singzi.hospappo.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HospitalServiceImpl implements HospitalService {
    @Autowired
    private HospitalRepository hospitalRepository;
    @Autowired
    private DictFeignClient dictFeignClient;

    @Override
    public void save(Map<String, Object> paraMap) {

        //参数map转换成对象hospital
        //map转字符串
        String mapString = JSONObject.toJSONString(paraMap);
        //字符串转对象
        Hospital hospital = JSONObject.parseObject(mapString, Hospital.class);

        //判断是否存在数据
        String hoscode = hospital.getHoscode();
        Hospital hospitalExist = hospitalRepository.getHospitalByHoscode(hoscode);
        //如果不存在，进行添加
        if (hospitalExist != null) {
            hospital.setStatus(hospitalExist.getStatus());
            hospital.setCreateTime(hospitalExist.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        } else {
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }

        //如果存在，进行修改
    }

    @Override
    public Hospital getByHoscode(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        return hospital;
    }

    //分页查询
    @Override
    public Page<Hospital> selectHospPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        //1 创建pageable对象
        Pageable pageable = PageRequest.of(page - 1, limit);
        //2 创建条件配置器
        ExampleMatcher exampleMatcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);

        //3 HSQ 对象转换成hospital
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo, hospital);

        // 4.创建对象
        Example<Hospital> example = Example.of(hospital, exampleMatcher);

        //5. 调用方法进行查询
        Page<Hospital> hospitals = hospitalRepository.findAll(example, pageable);
        List<Hospital> content = hospitals.getContent();
        content.stream().forEach(item -> {
            this.setHospitalHosType(item);
        });
        return hospitals;
    }

    //更新医院状态
    @Override
    public void updateStatus(String id, Integer status) {
        Hospital hospital = hospitalRepository.findById(id).get();
        hospital.setStatus(status);
        hospital.setUpdateTime(new Date());
        hospitalRepository.save(hospital);
    }

    @Override
    public Map<String, Object> getHospitalById(String id) {
        Map<String, Object> result = new HashMap<>();
        Hospital hospitalTemp = hospitalRepository.findById(id).get();
        Hospital hospital = this.setHospitalHosType(hospitalTemp);
        //包含医院等级的集基本信息
        result.put("hospital", hospital);
        //预约规则
        result.put("bookingRule", hospital.getBookingRule());
        hospital.setBookingRule(null);
        return result;
    }

    @Override
    public String getHospName(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        if(hospital != null) {
            return hospital.getHosname();
        }
        return null;
    }

    @Override
    public List<Hospital> findByHosname(String hosname) {
        return hospitalRepository.findHospitalByHosnameLike(hosname);
    }

    private Hospital setHospitalHosType(Hospital hospital) {
        String hostypeString = dictFeignClient.getName("Hostype", hospital.getHostype());
        //查询省 市  地区
        String provinceString = dictFeignClient.getName(hospital.getProvinceCode());
        String cityString = dictFeignClient.getName(hospital.getCityCode());
        String districtString = dictFeignClient.getName(hospital.getDistrictCode());

        hospital.getParam().put("fullAddress",provinceString+cityString+districtString);
        hospital.getParam().put("hostypeString", hostypeString);
        return hospital;

    }
}